package com.mzy.datepackage.M2022.May.teneight;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author mzy
 * @Date 2022/5/19 20:25
 * @Version 1.0
 */

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class Main {

    public static void main(String[] args) throws UnknownHostException, SocketException {
        //获取本机IP地址
        String ip = getIpAddress();
        //获取本机网卡的MAC地址
        String mac = getMacAddress();
        log.info("IP地址为：{}, 本机网卡MAC地址为：{}", ip, mac);
    }

    /**
     * 获取本机IP地址
     */
    private static String getIpAddress() throws UnknownHostException {
        InetAddress ia = InetAddress.getLocalHost();
        return ia.getHostAddress();
    }

    /**
     * 获取本机使用网卡的MAC地址
     */
    private static String getMacAddress() throws UnknownHostException, SocketException {
        //获取IP地址，输出示例：WCGZ-DZ-013803/10.88.12.117
        InetAddress ia = InetAddress.getLocalHost();
        //获取网卡的MAC地址
        byte[] mac = NetworkInterface.getByInetAddress(ia).getHardwareAddress();
        StringBuffer sb = new StringBuffer("");
        for (int i = 0; i < mac.length; i++) {
            if (i != 0) {
                sb.append("-");
            }
            //字节转换为整数
            int temp = mac[i] & 0xff;
            String str = Integer.toHexString(temp);
            if (str.length() == 1) {
                sb.append("0" + str);
            } else {
                sb.append(str);
            }
        }
        return sb.toString().toUpperCase();
    }
}